package com.sec.android.app.myfiles.info;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.DisplayMetrics;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.util.UiUtils;

public class AppFeatures {
    private static final String TAG = "AppFeatures";

    //TODO HYL 判断SDCard
    //public static final boolean SUPPORT_SD_CARD = SemSystemProperties.getInt("storage.support.sdcard", -1) == 1;
    public static final boolean SUPPORT_SD_CARD = false;
    //public static final boolean IS_FACTORY_MODE = "factory".equalsIgnoreCase(SemSystemProperties.get("ro.factory.factory_binary", "Unknown"));
    public static final boolean IS_FACTORY_MODE = false;

    public static final int SIP_APPEARANCE_DELAY = 200;
    private static final int MEDIA_DB_VERSION_SUPPORTING_IS_DOC_FIELD = 825;
    private static final int MEDIA_DB_VERSION_SUPPORTING_DATA_ACCESSED_FIELD = 829;
    private static final float DISPLAY_ASPECT_RATIO_DIVIDE_THRESHOLD = 1.8f;
    public static final int SUPPORT_MOUSE_POINTER_SEM_VERSION = 2403;


    /**
     * Regarding SemFloatingFeature
     */
    //private static final SemFloatingFeature FLOATING_FEATURE = SemFloatingFeature.getInstance();

    /*
    public static final boolean SUPPORT_FW_AIR_VIEW_FINGER_PREVIEW = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_COMMON_GESTURE_WITH_FINGERHOVER");
    public static final boolean SUPPORT_KNOX_DESKTOP = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_COMMON_SUPPORT_KNOX_DESKTOP");
    public static final boolean SUPPORT_GEAR360 = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_COMMON_SUPPORT_GEAR360");
    public static final boolean SUPPORT_IA = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_COMMON_SUPPORT_BIXBY");
    public static final boolean DSD_ENABLED = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_MMFW_SUPPORT_DSD_FORMAT");
    public static final boolean APE_ENABLED = FLOATING_FEATURE != null && FLOATING_FEATURE.getBoolean("SEC_FLOATING_FEATURE_MMFW_SUPPORT_APE_FORMAT");
    private static final String QUICKCONNECT_PACKAGE_NAME = FLOATING_FEATURE != null ? FLOATING_FEATURE.getString("SEC_FLOATING_FEATURE_QUICKCONNECT_CONFIG_PACKAGE_NAME", "com.samsung.android.oneconnect") : "com.samsung.android.oneconnect";
    */

    public static final boolean SUPPORT_FW_AIR_VIEW_FINGER_PREVIEW = false;
    public static final boolean SUPPORT_KNOX_DESKTOP = false;
    public static final boolean SUPPORT_GEAR360 = false;
    public static final boolean SUPPORT_IA = false;
    public static final boolean DSD_ENABLED = false;
    public static final boolean APE_ENABLED = false;
    private static final String QUICKCONNECT_PACKAGE_NAME = "";


    /**
     * Regarding SemCscFeature
     */
    //private static final SemCscFeature CSC_FEATURE = SemCscFeature.getInstance();
    /*
    public static final boolean IS_VZW = CSC_FEATURE != null && CSC_FEATURE.getString("CscFeature_MyFiles_ConfigOpCloud").equalsIgnoreCase("vz");
    public static final boolean IS_JPN = CSC_FEATURE != null && CSC_FEATURE.getBoolean("CscFeature_Common_ReplaceSecBrandAsGalaxy");
    public static final boolean SUPPORT_PINLOCK = CSC_FEATURE != null && CSC_FEATURE.getBoolean("CscFeature_MyFiles_SupportPinLock");
    public static final boolean NO_SUPPORT_CLOUD = CSC_FEATURE != null && CSC_FEATURE.getString("CscFeature_MyFiles_ConfigOpCloud").equalsIgnoreCase("alibaba");
    public static final boolean SUPPORT_FOLDER_DESCRIPTION = CSC_FEATURE != null && CSC_FEATURE.getBoolean("CscFeature_MyFiles_SupportFolderDescription");
    public static final boolean SCROLL_DA_ENABLED = CSC_FEATURE != null && CSC_FEATURE.getBoolean("CscFeature_Common_SupportMinimizedSip");
    public static final boolean SUPPORT_QCP = CSC_FEATURE != null && CSC_FEATURE.getString("CscFeature_MMFW_ConfigFileExtension").toLowerCase().contains("qcp");
    */

    //TODO HYL  地区项的判断
    public static final boolean IS_VZW = false;
    public static final boolean IS_JPN = false;
    public static final boolean SUPPORT_PINLOCK = false;
    public static final boolean NO_SUPPORT_CLOUD = true;
    public static final boolean SUPPORT_FOLDER_DESCRIPTION = true;
    public static final boolean SCROLL_DA_ENABLED = false;
    public static final boolean SUPPORT_QCP = false;


    /**
     * From N OS, except scafe.version, scafe.syrup, ro.build.scafe.* properties are all deleted
     * So, To compare features, we have to use ro.product.name property.
     */
    //private static final String PRODUCT_NAME = SemSystemProperties.get("ro.product.name");
    private static final String PRODUCT_NAME = "";
    private static final int CLOUD_GATEWAY_VERSION_CODE_SUPPORTING_SAMSUNG_DRIVE = 210000000;

    private static boolean sIsSupportIntensity = false;
    private static boolean sIsMassModel; // temporary feature for mass model
    private static boolean sIsAFW;
    private static boolean sIsAFWBYOD;
    private static boolean sIsKnox = false;
    private static boolean sIsTablet = false;
    private static boolean sIsNoFocusTabletUI = false;
    private static Boolean sIsSupportFolderShare = null;
    private static Boolean sIsSupportDocumentType = null;
    private static Boolean sIsSupportDataAccessed = null;
    private static Boolean sIsNote = null;
    private static boolean sIsKnoxDesktopMode;
    private static Boolean sIsSupportRecentTitleModel = null;
    private static Boolean sIsBlockZipErrorModel = null;
    private static boolean sIsCloudGatewaySupportSamsungDrive = false;
    private static Boolean sIsSupportDesktopModeListener = null;

    private AppFeatures() {
    }

    public static boolean isSupportIntensity() {
        return sIsSupportIntensity;
    }

    public static boolean isMassModel() {
        return sIsMassModel;
    }

    public static boolean isAndroidForWork() {
        return sIsAFW;
    }

    public static boolean isBYOD() {
        return sIsAFWBYOD;
    }

    public static boolean isKnox() {
        return sIsKnox;
    }

    public static boolean isTablet() {
        return sIsTablet;
    }

    public static boolean isNoFocusTabletUI() {
        return sIsNoFocusTabletUI;
    }

    private static void setTabletInfo() {

        /*
        String deviceType = SemSystemProperties.get("ro.build.characteristics");
        if (deviceType != null && deviceType.contains("tablet")) {
            sIsTablet = true;
        }
        String[] NO_FOCUS_TAB_LIST = {
                "gta2slte", "gtactive2", "gt5note8lte", "gt5note10lte", "gtaxlqlte", "lugeltekx"
        };

        if (PRODUCT_NAME != null) {
            for (String tabModel : NO_FOCUS_TAB_LIST) {
                if (PRODUCT_NAME.startsWith(tabModel)) {
                    sIsNoFocusTabletUI = true;
                    break;
                }
            }
        }
        */
    }

    public static void setKnoxDesktopMode(boolean isDesktopMode) {
        sIsKnoxDesktopMode = isDesktopMode;
    }

    public static boolean isKnoxDesktopMode() {
        boolean ret = false;
        if (SUPPORT_KNOX_DESKTOP) {
            ret = sIsKnoxDesktopMode;
        }
        return ret;
    }

    public static void initMode(Context context) {
        sIsMassModel = false; // To distinguish, new feature or standard is

        // needed.
        sIsKnox = false;
        sIsAFWBYOD = false;
        sIsAFW = false;
        setTabletInfo();

        sIsCloudGatewaySupportSamsungDrive = isCloudGatewaySupportSamsungDrive(context);

        Log.d(TAG, "MASS = " + sIsMassModel + " , BYOD = " + sIsAFWBYOD + " , AFW:" + sIsAFW + " , KNOX = " + sIsKnox);
        Log.d(TAG, "TABLET = " + sIsTablet + " , supportIntensity = " + sIsSupportIntensity + " , sIsKnoxDesktopMode = " + sIsKnoxDesktopMode);
    }

    /**
     * Accessibility : Vision - Show Button Background
     */
    public static boolean isEnableButtonBackgrounds(Context context) {
        return (Settings.System.getInt(context.getContentResolver(), "show_button_background", 0) > 0);
    }

    private static boolean isCloudGatewaySupportSamsungDrive(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo info = null;
        try {
            info = packageManager.getPackageInfo("com.samsung.android.slinkcloud", 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        // CloudGateway supports Samsung drive from 2.1.0
        return info != null ? info.versionCode >= CLOUD_GATEWAY_VERSION_CODE_SUPPORTING_SAMSUNG_DRIVE : false;
    }

    public static boolean isCloudGatewaySupportSamsungDrive() {
        return sIsCloudGatewaySupportSamsungDrive;
    }

    public static boolean isSupportSamsungDrive() {
        return !IS_VZW && !isChinaModel() && sIsCloudGatewaySupportSamsungDrive;
    }

    public static boolean isInstalledQuickConnect(Context context) {
        try {
            context.getPackageManager().getPackageInfo(QUICKCONNECT_PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
        } catch (NameNotFoundException e) {
            Log.e(TAG, "NameNotFoundException:" + e.toString());
            return false;
        } catch (NullPointerException e) {
            Log.e(TAG, "NullPointerException:" + e.toString());
            return false;
        }
        Log.d(TAG, "QuickConnect is installed");
        return true;
    }

    /**
     * For the Enhanced document query()
     */
    private static int getMediaDBVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo("com.android.providers.media", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            Log.d(TAG, "couldn't get the versionCode for com.android.providers.media");
            return -1;
        }
    }

    public static boolean isSupportDocumentType(Context context) {
        if (sIsSupportDocumentType == null) {
            if (getMediaDBVersionCode(context) >= MEDIA_DB_VERSION_SUPPORTING_IS_DOC_FIELD) {
                sIsSupportDocumentType = true;
            } else {
                sIsSupportDocumentType = false;
            }
        }
        return sIsSupportDocumentType;
    }

    /**
     * For Unused file check
     */
    public static boolean isSupportDataAccessed(Context context) {
        if (sIsSupportDataAccessed == null) {
            if (getMediaDBVersionCode(context) >= MEDIA_DB_VERSION_SUPPORTING_DATA_ACCESSED_FIELD) {
                sIsSupportDataAccessed = true;
            } else {
                sIsSupportDataAccessed = false;
            }
        }
        return sIsSupportDataAccessed;
    }

    public static boolean isSupportFolderShare(Context context) {
        if (sIsSupportFolderShare == null) {
            sIsSupportFolderShare = false;
            ApplicationInfo appInfo;
            try {
                appInfo = context.getPackageManager().getApplicationInfo("com.samsung.android.allshare.service.fileshare", PackageManager.GET_META_DATA);
                if (appInfo.metaData.getBoolean("support.feature.folder_transfer", false) && !UiUtils.isUPSM(context)) {
                    sIsSupportFolderShare = true;
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return sIsSupportFolderShare;
    }

    public static boolean isNote() {
        if (sIsNote == null) {
            boolean isNote = false;
            String[] NOTE_MODEL_LIST = {
                    "hlte", "ha3g", "h3g", "hl3g", "hllte", "ms01", "ASH", "SC-01F", "Madrid", "SCL22", "JS01", "SC-02F", "flte", "fresco", "melius", "crater", "flte",
                    "ms01", "h3gduosctc", "t0", "noblelte", "a8", "SCV32", "zen", "a9", "hero2",
                    "SC-02H", "SGH-N611", "SCV33", "KSU", "j7", "a7", "grace", "SC-01J", "SCV34", "LTV", "dream2", "SC-03J", "SCV35", "great", "c10p", "jade", "c9",
                    "SC-01K", "SCV37", "c7", "jackpot"
            };

            if (PRODUCT_NAME != null) {
                for (String noteModel : NOTE_MODEL_LIST) {
                    if (PRODUCT_NAME.startsWith(noteModel) || PRODUCT_NAME.equalsIgnoreCase(noteModel)) {
                        isNote = true;
                        break;
                    }
                }
            }
            sIsNote = isNote;
        }

        return sIsNote;
    }

    public static boolean isTabletUIMode() {
        return isTablet() || isKnoxDesktopMode();
    }

    public static boolean isChinaModel() {
        return SUPPORT_FOLDER_DESCRIPTION;
    }

    public static boolean isExtendedScreen(Context context) {
        DisplayMetrics m = context.getResources().getDisplayMetrics();
        float screenRatio = ((float) (Math.max(m.widthPixels, m.heightPixels)) / (float) (Math.min(m.widthPixels, m.heightPixels)));
        return screenRatio > DISPLAY_ASPECT_RATIO_DIVIDE_THRESHOLD;
    }

    public static boolean supportRecentTitleModel() {
        if (sIsSupportRecentTitleModel == null) {
            sIsSupportRecentTitleModel = false;
            String[] SUPPORT_RECENT_TITLE_MODEL = {"j7max", "jade", "jackpot"};

            if (PRODUCT_NAME != null) {
                for (String recentTitleModel : SUPPORT_RECENT_TITLE_MODEL) {
                    if (PRODUCT_NAME.startsWith(recentTitleModel) || PRODUCT_NAME.equalsIgnoreCase(recentTitleModel)) {
                        sIsSupportRecentTitleModel = true;
                        break;
                    }
                }
            }
        }

        return sIsSupportRecentTitleModel;
    }

    public static boolean blockZipErrorModel() {
        if (sIsBlockZipErrorModel == null) {
            sIsBlockZipErrorModel = Boolean.valueOf(false);
            String[] SUPPORT_ZIP_ERROR_MODEL = {"on7xreflte", "on5xreflte", "j5xnlte", "gta2s", "gt58", "gteslte"};

            if (PRODUCT_NAME != null) {
                for (String zipErrorModel : SUPPORT_ZIP_ERROR_MODEL) {
                    if (PRODUCT_NAME.startsWith(zipErrorModel) || PRODUCT_NAME.equalsIgnoreCase(zipErrorModel)) {
                        sIsBlockZipErrorModel = Boolean.valueOf(true);
                        break;
                    }
                }
            }
        }
        if (sIsBlockZipErrorModel != null) {
            return sIsBlockZipErrorModel.booleanValue();
        } else {
            return false;
        }
    }


    public static boolean isSupportDesktopModeListener() {
        return false;
    }
}
